<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ConfigController
 *
 * @author Administrator
 */
class ConfigController extends Controller {

    public $model;
    public $message;

    public function actionIndex() {
        $this->message = '';
        $model = new Config();

        $this->render('index', array('model' => $model));
    }

    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'config-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

    public function actionCreate() {
        $model = new Config();
        $this->performAjaxValidation($model);
        if (Yii::app()->request->getIsPostRequest()) {
            $result = Yii::app()->request->getPost('Config');
            $model->attributes = $result;
            if ($model->save()) {
                $this->render('/layouts/message', array( 'message' => '添加成功'));
                Yii::app()->end();
            }
        }
        $this->render('create', array('model' => $model));
    }

    public function actionUpdate() {
        $id = intval(Yii::app()->request->getParam("id"));
        $config = new Config();
        $modify = $config->findByPk($id);
        if (Yii::app()->request->isPostRequest) {
            $modify->attributes = Yii::app()->request->getPost('Config');
            if ($modify->save()) {
                $this->redirect($this->createUrl('index'));
            }
        }

        $this->render('update', array('modify' => $modify));
    }

    public function actionDelete() {
        $id = Yii::app()->request->getParam("id");
        if ($id) {
            $config = new Config();
            if (is_array($id)) {
                foreach($id as $pk)
                    $config->deleteByPk((int)$pk);
            } else {
                $config->deleteByPk((int)$id);
            }
            echo RMessage::set('true');
        }else{
            echo RMessage::set('false', Yii::t('default', '编号未接收到'));
        }
    }

}